<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (the "License"); you may not use this file except in compliance with
   - the License. You may obtain a copy of the License at
   - http://www.mozilla.org/MPL/
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is PyXPCOM.
   -
   - The Initial Developer of the Original Code is
   - ActiveState Tool Corporation.
   - Portions created by the Initial Developer are Copyright (C) 2000-2001
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the LGPL or the GPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   -
   - ***** END LICENSE BLOCK ***** -->

<center><b><font size=+2>Python Component Sample</font></b>

<p>
<br>
Last modified 
<script>
document.write(document.lastModified);
</script>
</center>

<p>XPConnect allows JavaScript
to transparantly access and manipulate XPCOM objects;

<p>Big Deal, I hear you say!  But it also works for Python!!!

<p>
This sample demonstrates accessing a XPCOM object through XPConnect.
The JavaScript executed when this page loads creates an instance
of the Python object by
using the <tt>Components</tt> object, then accesses it through
the <a href="py_test_component.idl">nsISample</a> interface by calling <tt>QueryInterface</tt>:
<br>
<pre>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var sample = Components.classes["component://mozilla/sample/sample-world"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsISample);
</pre>

<p>
The buttons on the form are connected to JavaScript event handlers which
call the methods defined in Python


<p><b><a name="Compiling">Compiling the idl</b>

<p>The XPIDL compiler (xpidl on Unix, xpidl.exe on Windows, and a CodeWarrior plugin on Mac)
is compiled at build time (except on Mac) thus
you will have to build mozilla in order to test this out. If you
have already built mozilla then the compiler will be located at <tt>mozilla\dist\WIN32_D.OBJ\bin\xpidl.exe</tt>.

<p>Once you have the XPIDL compiler enter the following command at your
prompt:
<br><tt>D:\whereever\xpcom\test\test_component>d:\mozilla\dist\WIN32_D.OBJ\bin\xpidl -I
d:\mozilla\dist\idl -m typelib py_test_component.idl</tt>.  You must then copy the generated .xpt file
to the mozilla component directory.

<p>The <tt>-I d:\mozilla\dist\idl</tt> points the compiler to the folder
containing the other idl files, needed because nsISample.idl inherits from
nsISupports.idl. The <tt>-m typelib</tt> instruction tells the compiler
to build the .XPT typelib file.</tt>. 

<p>
For more information on compilation see the <a href="http://www.mozilla.org/scriptable/xpidl/">xpidl
compiler page</a>.

<p><b>Running the sample</b>
<p><b>NOTE: This doesn't work for me - I get an access denied error using XPConnect!</b>
<p>Using Mozilla, load this file. Pay attention
to the console when clicking "write".

<!-- XXX keep in sync with stuff in pre tag below -->
<script>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var sample = Components.classes["Python.TestComponent"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsIPythonTestInterface);
dump("sample = " + sample + "\n");

function get()
{
  var field = document.getElementById('Value');
  field.value = sample.str_value;
}

function set()
{
  var field = document.getElementById('Value');
  sample.str_value = field.value;
}

function poke()
{
  var field = document.getElementById('Value');
  sample.poke(field.value);
}

function write()
{
  sample.writeValue("here is what I'm writing: ");
}
</script>

<p>
<form name="form">
<input type="button" value="Get" onclick="get();">
<input type="button" value="Set" onclick="set();">
<input type="button" value="Poke" onclick="poke();">
<input type="text" id="Value">
<input type="button" value="Write" onclick="write();">
<form>

<hr>

<p>
JavaScript and form source:

<!-- XXX keep in sync with actual script -->
<pre>
&lt;script&gt;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var sample = Components.classes["component://Python.TestComponent"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsIPythonTestInterface);
dump("sample = " + sample + "\n");

function get()
{
  var field = document.getElementById('Value');
  field.value = sample.str_value;
}

function set()
{
  var field = document.getElementById('Value');
  sample.str_value = field.value;
}

function poke()
{
  var field = document.getElementById('Value');
  sample.poke(field.value);
}

function write()
{
  sample.writeValue("here is what I'm writing: ");
}
&lt;/script&gt;

&lt;form name=&quot;form&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Get&quot; onclick=&quot;get();&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Set&quot; onclick=&quot;set();&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Poke&quot; onclick=&quot;poke();&quot;&gt;
&lt;input type=&quot;text&quot; id=&quot;Value&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Write&quot; onclick=&quot;write();&quot;&gt;
&lt;form>

</pre>
